package com.gdth.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;

public class PermissionUtil {
	private static final int REQUEST_EXTERNAL_STORAGE = 1;

	private static String[] PERMISSIONS_STORAGE = {
			Manifest.permission.READ_EXTERNAL_STORAGE,
			Manifest.permission.WRITE_EXTERNAL_STORAGE };
	
	private static String[] PERMISSIONS_CAMERA = { Manifest.permission.CAMERA };
	private static String[] PERMISSIONS_RECORD_AUDIO = { Manifest.permission.RECORD_AUDIO };
	private static String[] PERMISSIONS_ACCESS_FINE_LOCATION = { Manifest.permission.ACCESS_FINE_LOCATION };
	private static String[] PERMISSIONS_READ_CONTACTS = { Manifest.permission.READ_CONTACTS };

	/**
	 * 是否有SD权限
	 * 
	 * @return
	 */
	public static boolean checkPermissionFile(Activity mActivity) {

		int permission = ActivityCompat.checkSelfPermission(mActivity,
				Manifest.permission.WRITE_EXTERNAL_STORAGE);
		if (permission != PackageManager.PERMISSION_GRANTED) {

			ActivityCompat.requestPermissions(mActivity, PERMISSIONS_STORAGE,
					REQUEST_EXTERNAL_STORAGE);

			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否有拍照权限
	 * 
	 * @param mActivity
	 * @return
	 */
	public static boolean checkPermissionCamera(Activity mActivity) {

		int permission = ActivityCompat.checkSelfPermission(mActivity,
				Manifest.permission.CAMERA);
		if (permission != PackageManager.PERMISSION_GRANTED) {

			ActivityCompat.requestPermissions(mActivity, PERMISSIONS_CAMERA,
					REQUEST_EXTERNAL_STORAGE);

			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否定位权限
	 * 
	 * @param mActivity
	 * @return
	 */
	public static boolean checkPermissionLocation(Activity mActivity) {

		int permission = ActivityCompat.checkSelfPermission(mActivity,
				Manifest.permission.ACCESS_FINE_LOCATION);
		if (permission != PackageManager.PERMISSION_GRANTED) {

			ActivityCompat.requestPermissions(mActivity,
					PERMISSIONS_ACCESS_FINE_LOCATION, REQUEST_EXTERNAL_STORAGE);

			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 检测是否有录音
	 * 
	 * @param mActivity
	 * @return
	 */
	public static boolean checkPermissionAudio(Activity mActivity) {

		int permission = ActivityCompat.checkSelfPermission(mActivity,
				Manifest.permission.RECORD_AUDIO);
		if (permission != PackageManager.PERMISSION_GRANTED) {

			ActivityCompat.requestPermissions(mActivity,
					PERMISSIONS_RECORD_AUDIO, REQUEST_EXTERNAL_STORAGE);

			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 获取打电话权限
	 * @param mActivity
	 * @return
	 */
	public static boolean checkPermissionPhone(Activity mActivity) {
		
		int permission = ActivityCompat.checkSelfPermission(mActivity,
				Manifest.permission.READ_CONTACTS);
		if (permission != PackageManager.PERMISSION_GRANTED) {
			
			ActivityCompat.requestPermissions(mActivity,
					PERMISSIONS_READ_CONTACTS, REQUEST_EXTERNAL_STORAGE);
			
			return true;
		} else {
			return false;
		}
	}
}
